package com.brillilab.service.job.instrument;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.service.core.instrument.IInstrumentService;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.module.message.template.impl.instrument.AppointmentAlertTemplate;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
@Slf4j
public class AppointmentAlert implements Job {

    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private IInstrumentService instrumentService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //TODO
        JobDataMap map=context.getJobDetail().getJobDataMap();
        String appstr=map.getString("appointment");
        Long labId=map.getLong("labId");
        String labName= map.getString("labName");
        Long receiverId= map.getLong("receiverId");
        long labMemberId=map.getLong("labMemberId");
        InstrumentAppointment appointment=JSONObject.parseObject(appstr,InstrumentAppointment.class);

        if(appointment!=null){
            Instrument instrument=instrumentService.selectById(appointment.getInstrumId());
            Assert.notNull(instrument,"仪器不存在！");
            AppointmentAlertTemplate appointmentAlertTemplate=new AppointmentAlertTemplate(receiverId,labMemberId,labId,labName,instrument,appointment);
            messageProducer.sendMessagePush(appointmentAlertTemplate);
        }

    }
}
